package com.example.ponycui_home.svgaplayer;

import com.opensource.svgaplayer.SVGAParser;
import com.opensource.svgaplayer.utils.PixelUtil;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import ohos.agp.utils.TextTool;
import ohos.agp.window.service.DisplayManager;
import java.util.ArrayList;

/**
 * ex.
 *
 * @author wl
 * @since 2021-06-22
 */
public class MainAbility extends Ability {
    ListContainer listView;
    private ArrayList<SampleItem> items = new ArrayList();

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        PixelUtil.initContext(this);
        setUIContent(ResourceTable.Layout_ability_main);
        this.setupData();
        this.setupListView();
        this.setupSVGAParser();
    }

    void setupData() {
        this.items.add(new SampleItem("Animation From Assets", AnimationFromAssetsAbility.class.getName()));
        this.items.add(new SampleItem("Animation From Network", AnimationFromNetworkAbility.class.getName()));
        this.items.add(new SampleItem("Animation From Layout XML", AnimationFromLayoutAbility.class.getName()));
        this.items.add(new SampleItem("Animation With Dynamic Image",  AnimationWithDynamicImageAbility.class.getName()));
        this.items.add(new SampleItem("Animation With Dynamic Click", AnimationFromClickAbility.class.getName()));
    }

    void setupListView() {
        listView = (ListContainer) findComponentById(ResourceTable.Id_listContainer);
        this.listView.setItemProvider(new BaseItemProvider() {
            @Override
            public int getCount() {
                return items.size();
            }

            @Override
            public Object getItem(int i) {
                return items.get(i);
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            @Override
            public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
                DirectionalLayout linearLayout = new DirectionalLayout(MainAbility.this);
                DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
                linearLayout.setLayoutConfig(layoutConfig);

                Text textView = new Text(MainAbility.this);
                textView.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component view) {
                        jumpToPage(items.get(i).pageName);
                    }
                });
                textView.setText(MainAbility.this.items.get(i).title);
                textView.setTextSize((int)PixelUtil.fp2px(18));
                textView.setTextColor(Color.BLACK);
                textView.setTextAlignment(TextAlignment.CENTER);
                linearLayout.addComponent(textView, new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, (int)PixelUtil.vp2px(55) ));
                return linearLayout;
            }
        });

    }

   static class SampleItem {

        String title;
        String pageName;

        public SampleItem(String title, String pageName) {
            this.title = title;
            this.pageName = pageName;
        }

    }

    void setupSVGAParser() {
        SVGAParser.shareParser().init(this);
    }

    private void jumpToPage(String pageName) {
        if (TextTool.isNullOrEmpty(pageName)) {
            return;
        }
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getBundleName())
                .withAbilityName(pageName)
                .build();
        intent.setOperation(operation);
        startAbility(intent);
    }
}
